package me.heyclock.hl.recordwriter.app;

import android.content.Intent;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.heyclock.hl.recordwriter.R;

public class SplashActivity extends AppCompatActivity {

    @BindView(R.id.asp_splash)
    public ConstraintLayout splash;

    private Timer timer = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0){
            finish();
            return;
        }
        // if(!isTaskRoot())，判断该Activity是不是任务空间的源Activity
        // “非”也就是说是被系统重新实例化出来，如果是就放在launcher
        // Activity中话，这里可以直接return了。
        //        if (!isTaskRoot()) {
        //            finish();
        //            return;
        //        }
        ///< 取消标题栏 - 属性里面已经设置了
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        ///< 取消状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        ///< 背景透明度变化1秒内从0.3变到1.0
        AlphaAnimation aa = new AlphaAnimation(0.5f, 1.0f);
        aa.setDuration(2000);
        splash.startAnimation(aa);

        ///< 创建Timer对象
        timer = new Timer();
        ///< 创建TimerTask对象
        final TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
                startActivity(intent);
                finish();
            }
        };
        timer.schedule(timerTask, 2000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != timer){
            timer.cancel();
            timer = null;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK &&
            event.getAction() == KeyEvent.ACTION_DOWN){
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
